package b2.player
{
	import b2.config.*;
	import b2.net.*;
	
	public class PlayerInfo
	{
		//玩家本人的ID号唯一号
		public var id:int;
		//出战坦克的ID
		public var campTankId:int;
		//玩家本人的QQ ID
		public var openId:String;
		//是否黄钻
		public var is_yellow_vip:Boolean;
		//是否年费黄钻
		public var is_yellow_year_vip:Boolean;
		//黄钻等级
		public var yellow_vip_level:int; 
		//玩家名字
		public var nickname:String;
		//玩家头像
		public var figureurl:String;
		//物品清单
		public var objPackage:ObjPackage;
		//坦克清单
		public var tankPackage:TankPackage;
		//任务清单
		public var taskPackage:TaskPackage;
		//完成战役清单
		public var campPackage:CampPackage;
		//邮件清单
		public var mailPackage:MailPackage;
		//社交清单
		public var socialPackage:SocialPackage;
		//人物状态
		public var status:Status;
		//是否完成新手引导
		public var finishGuide:Boolean;
		//登陆天数
		public var logonDayCount:int;
		//领取登录奖励的时间
		public var fetchGiftDate:int;
		//是否已领取新手礼包
		public var fetchNewPlayerGift:Boolean;
		//本日被攻击的次数
		public var attackedCount:int;
		//当前等级
		public var currentLevel:int;
		
		public function PlayerInfo(playerXML:XML = null)
		{
			this.objPackage = new ObjPackage();
			this.tankPackage = new TankPackage();
			this.taskPackage = new TaskPackage();
			this.campPackage = new CampPackage();
			if (playerXML != null)
			{
				this.id = playerXML.playerId;
				this.campTankId = playerXML.campTankId;
				this.openId = playerXML.openId;
				this.is_yellow_vip = Boolean(parseInt(playerXML.is_yellow_vip));
				this.is_yellow_year_vip = Boolean(parseInt(playerXML.is_yellow_year_vip));
				this.yellow_vip_level = playerXML.yellow_vip_level;
				this.nickname = playerXML.nickname;
				this.figureurl = playerXML.figureurl;
				this.finishGuide = Boolean(parseInt(playerXML.finishGuide));
				this.logonDayCount = playerXML.logonDayCount;
				this.fetchGiftDate = playerXML.fetchGiftDate;
				this.fetchNewPlayerGift = Boolean(parseInt(playerXML.fetchNewPlayerGift));
				this.attackedCount = playerXML.attackedCount;
				this.currentLevel = playerXML.currentLevel;
			}
		}
		
		public static function getTitleInfo(hr:int):Array
		{
			var _title:int = 0;
			while (_title < Config.levelConfigs.length && hr >= Config.levelConfigs[_title].honour)
			{
				hr -= Config.levelConfigs[_title].honour;
				_title++;
			}
			return [_title + 1, hr, Config.levelConfigs[_title].title];
		}
		
		//军衔
		public function get titleName():String
		{
			var hr:int = getObjNum(CommonObjConfig.HONOUR, 0, 0);
			var titleInfo:Array = getTitleInfo(hr);
			return titleInfo[2];
		}
		
		//军衔等级
		public function get title():int
		{
			var hr:int = getObjNum(CommonObjConfig.HONOUR, 0, 0);
			var titleInfo:Array = getTitleInfo(hr);
			return titleInfo[0];
		}
		
		//荣誉
		public function get honour():int
		{
			var hr:int = getObjNum(CommonObjConfig.HONOUR, 0, 0);
			var titleInfo:Array = getTitleInfo(hr);
			return titleInfo[1];
		}
		
		public function get level():int
		{
			var exp:int = getObjNum(CommonObjConfig.EXP, 0, 0);
			var _level:int = 0;
			while (_level < Config.levelConfigs.length && exp >= Config.levelConfigs[_level].experience)
			{
				exp -= Config.levelConfigs[_level].experience;
				_level++;
			}
			return _level + 1;
		}
		
		public function get experience():int
		{
			var exp:int = getObjNum(CommonObjConfig.EXP, 0, 0);
			var _level:int = 0;
			while (_level < Config.levelConfigs.length && exp >= Config.levelConfigs[_level].experience)
			{
				exp -= Config.levelConfigs[_level].experience;
				_level++;
			}
			return exp;
		}
		
		public function get isAdmin():Boolean
		{
			return Config.basicConfig.adminOpenId.indexOf(openId) >= 0;
		}
		
		public function hasFetchedGift():Boolean
		{
			return (Math.floor(fetchGiftDate / 86400) == ServerHelper.getInstance().getClientDate())
		}
		
		public function getCampNum(campIndex:int):int
		{
			if (campPackage.campList[campIndex] == null)
			{
				return 0;
			}
			else
			{
				return campPackage.campList[campIndex];
			}
		}
		
		public function getTankNum(basicLevel:int, strengthenLevel:int):int
		{
			var count:int = 0;
			for (var i:int = 0; i < this.tankPackage.tankList.length; i++)
			{
				var tankInfo:TankInfo = PlayerInfoManager.getInstance().tanks[tankPackage.tankList[i]];
				if (tankInfo != null && tankInfo.basicLevel == basicLevel && tankInfo.strengthenLevel == strengthenLevel)
				{
					count++;
				}
			}
			return count;
		}
		
		public function getFinishTaskTankNum(basicLevel:int, strengthenLevel:int):int
		{
			var count:int = 0;
			for (var i:int = 0; i < this.tankPackage.tankList.length; i++)
			{
				var tankInfo:TankInfo = PlayerInfoManager.getInstance().tanks[tankPackage.tankList[i]];
				if (tankInfo != null && (tankInfo.basicLevel > basicLevel || (tankInfo.basicLevel == basicLevel && tankInfo.strengthenLevel >= strengthenLevel)))
				{
					count++;
				}
			}
			return count;
		}
		
		public function getObjNum(objectIndex:int, basicLevel:int, strengthenLevel:int):int
		{
			var compIndex:String = objectIndex + "_" + basicLevel + "_" + strengthenLevel + "_0";
			if (objPackage.objectList[compIndex] == null)
			{
				return 0;
			}
			else
			{
				return objPackage.objectList[compIndex];
			}
		}
		
		public function getFinishTaskObjNum(objectIndex:int, basicLevel:int, strengthenLevel:int):int
		{
			var count:int = 0;
			var objectIndexC:int;
			var basicLevelC:int;
			var strengthenLevelC:int;
			for (var objIndex:String in objPackage.objectList)
			{
				var array:Array = objIndex.split("_");
				if (array.length >= 3)
				{
					objectIndexC = parseInt(array[0]);
					basicLevelC = parseInt(array[1]);
					strengthenLevelC = parseInt(array[2]);
				}
				else
					continue;
				if (objectIndex == objectIndexC)
				{
					if (basicLevelC > basicLevel)
					{
						count += objPackage.objectList[objIndex];
					}
					else if (basicLevelC == basicLevel)
					{
						if (strengthenLevelC >= strengthenLevel)
							count += objPackage.objectList[objIndex];
					}
				}
			}
			return count;
		}
		public function checkObjEquiped(objectIndex:int, basicLevel:int, strengthenLevel:int):Boolean
		{
			for (var i:int = 0; i < this.tankPackage.tankList.length; i++)
			{
				var compIndex:String = objectIndex + "_" + basicLevel + "_" + strengthenLevel + "_" + tankPackage.tankList[i];
				if (objPackage.objectList[compIndex] != null)
				{
					return true;
				}
			}
			return false;
		}
		
		//获得新完成的任务
		public function getNewFinishedTask():int
		{
			var taskIndexes:Array = new Array();
			for (var taskIndex:String in taskPackage.taskList)
			{
				var taskConfig:TaskConfig = Config.taskConfigs[taskIndex];
				if (getTaskStatus(taskConfig.index) == 1)
				{
					return taskConfig.index;
				}
			}
			return -1;
		}
		
		public function getTaskStatus(taskIndex:int):int
		{
			var taskConfig:TaskConfig = Config.taskConfigs[taskIndex];
			//该任务不存在
			if (taskConfig == null)
			{
				return -3;
			}
			//任务不可接
			else if (taskConfig.requireLevel > level)
			{
				return -2;	
			}
			//任务未接
			else if (taskPackage.taskList[taskIndex] == null)
			{
				return -1;
			}
			//任务完结
			else if (taskPackage.taskList[taskIndex] >= 2)
			{
				return 2;
			}
			//任务进行中
			else
			{
				var basicLevel:int = taskConfig.objectQuality / 5;
				var strengthenLevel:int = taskConfig.objectQuality % 5;
				//战役类
				if (taskConfig.goal == 0)
				{
					if (getCampNum(taskConfig.campIndex) >= taskConfig.campNum)
						return 1;
				}
				//装备类
				else if (taskConfig.goal == 2)
				{
					if (checkObjEquiped(taskConfig.objectIndex, basicLevel, strengthenLevel))
						return 1;
				}
				//强化/合成坦克类
				else if (taskConfig.goal == 5 || taskConfig.goal == 6)
				{
					if (getFinishTaskTankNum(basicLevel, strengthenLevel) >= taskConfig.objectNum)
						return 1;
				}
				//收集类/强化/合成装备类
				else 
				{
					if (getFinishTaskObjNum(taskConfig.objectIndex, basicLevel, strengthenLevel) >= taskConfig.objectNum)
						return 1;
				}
				return 0;
			}
		}
		
	}
}